/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.activity;

import java.util.List;

import net.bezrabotnih.cabinet.client.place.ProfileEditPlace;
import net.bezrabotnih.cabinet.client.place.ProfilePlace;
import net.bezrabotnih.cabinet.client.request.CabinetRequestFactory;
import net.bezrabotnih.cabinet.client.request.CabinetUserAccountRequest;
import net.bezrabotnih.cabinet.client.ui.ProfileEditView;
import net.bezrabotnih.cabinet.client.ui.ProfileEditViewImpl;
import net.bezrabotnih.client.managed.request.JobTypeProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.Request;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class ProfileEditActivity extends AbstractActivity implements ProfileEditView.Presenter {

	/**
	 * Sample property.
	 */
	private String name;
	private CabinetRequestFactory requests;
	private PlaceController placeController;
	private ProfileEditView view; 
	private UserAccountProxy userAccountProxy;

	public ProfileEditActivity(ProfileEditPlace place,CabinetRequestFactory requests,PlaceController placeController) {
		this.name = place.getName();
		this.requests=requests;
		this.placeController=placeController;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		this.view = ProfileEditViewImpl.instance();
		view.setName(name);
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		initData();
	}

	@SuppressWarnings("deprecation")
	private void initData(){
		final Receiver<List<JobTypeProxy>> jobTypesProxiesReceiver=new Receiver<List<JobTypeProxy>>(){
			@Override
			public void onSuccess(List<JobTypeProxy> allJobTypeProxies) {
				view.initAllJobTypesList(allJobTypeProxies);
				if (userAccountProxy!=null){
					view.setJobTypes(userAccountProxy.getJobTypeIds());
				}
			}
		};
		
		Receiver<UserAccountProxy> userAccountProxyReceiver=new Receiver<UserAccountProxy>() {
			@Override
			public void onSuccess(UserAccountProxy userAccountProxy) {
				ProfileEditActivity.this.userAccountProxy=userAccountProxy;
				if (userAccountProxy.getAccountType()!=null) view.setAccountType(userAccountProxy.getAccountType()==0);
				if (userAccountProxy.getAge()!=null) view.setAge(userAccountProxy.getAge());
				if (userAccountProxy.getName()!=null) view.setName(userAccountProxy.getName());
				if (userAccountProxy.getPhoneNumber()!=null) view.setPhoneNumber(userAccountProxy.getPhoneNumber());
				if (userAccountProxy.getSex()!=null) view.setSex(userAccountProxy.getSex()==0);
				if (view.isAllJobTypesListInited()){
					view.setJobTypes(userAccountProxy.getJobTypeIds());
				}else{
					requests.cabinetJobTypeRequest().findAllJobTypes().fire(jobTypesProxiesReceiver);
				}
					
				
			}
		};
	
		requests.cabinetUserAccountRequest().currentUserAccount().fire(userAccountProxyReceiver);
	}
	
	
	
	@Override
	public String mayStop() {
		return null;
	}

	/**
	 * @see ProfileEditView.Presenter#goTo(Place)
	 */
	public void goTo(Place place) {
		// TODO
	}

	@SuppressWarnings("deprecation")
	@Override
	public void saveClicked() {
		if (userAccountProxy!=null){
			CabinetUserAccountRequest request=requests.cabinetUserAccountRequest();
			UserAccountProxy editableUserAccountProxy=request.edit(userAccountProxy);
			editableUserAccountProxy.setAccountType(view.getAccountType()?0:1);
			if (view.getAge()!=null) editableUserAccountProxy.setAge(view.getAge());
			if (view.getName()!=null&&!view.getName().equals("")) editableUserAccountProxy.setName(view.getName());
			if (view.getPhoneNumber()!=null) editableUserAccountProxy.setPhoneNumber(view.getPhoneNumber());
			editableUserAccountProxy.setSex(view.getSex()?0:1);
			editableUserAccountProxy.setJobTypeIds(view.getSelectedJobTypeIds());
			
			request.persist().using(editableUserAccountProxy).fire(new Receiver<Void>() {

				@Override
				public void onSuccess(Void response) {
					placeController.goTo(new ProfilePlace(view.getAccountType()?"performer":"provider"));
					
				}
			});
			
		}
		
		
	}
}
